package cn.sumpu.app.coolsk.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import cn.sumpu.app.coolsk.biz.ContentBiz;
import cn.sumpu.app.coolsk.biz.DiscussBiz;
import cn.sumpu.app.coolsk.biz.HotelRoomBiz;
import cn.sumpu.app.coolsk.biz.IdentifierBiz;
import cn.sumpu.app.coolsk.biz.ProductBiz;
import cn.sumpu.app.coolsk.biz.ScenicBiz;
import cn.sumpu.app.coolsk.dao.DiscussDao;
import cn.sumpu.app.coolsk.dao.MemberDao;
import cn.sumpu.app.coolsk.dao.PointDao;
import cn.sumpu.app.coolsk.domain.Content;
import cn.sumpu.app.coolsk.domain.Discuss;
import cn.sumpu.app.coolsk.domain.Hotel;
import cn.sumpu.app.coolsk.domain.Member;
import cn.sumpu.app.coolsk.domain.Point;
import cn.sumpu.app.coolsk.domain.Product;
import cn.sumpu.app.coolsk.domain.Scenic;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.toolkit.common.page.PageModel;

public class DiscussBizImpl implements DiscussBiz{
	
	@Autowired
	private HotelRoomBiz hotelRoomBiz;
	@Autowired
	private IdentifierBiz idBiz;
	@Autowired
	private ContentBiz contentBiz;
	@Autowired
	private ProductBiz productBiz;
	@Autowired
	private ScenicBiz scenicBiz;
	private DiscussDao discussDao;
	private PointDao pointDao;
	private MemberDao memberDao;
	
	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}
	public void setPointDao(PointDao pointDao) {
		this.pointDao = pointDao;
	}
	public void setDiscussDao(DiscussDao discussDao) {
		this.discussDao = discussDao;
	}
	/**
	 * 查询所有的点评
	 */
	@Override
	public PageModel getAllDiscuss(Integer pageSize, Integer page,String memberId) throws AppBusinessException {
		if(page==null || page<1){  
			page = 1;
		}
		if(pageSize==null || pageSize<1){
			pageSize = 10;
		} 
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("member", memberId);
		map.put("pageSize", pageSize);
		map.put("startId", (page - 1) * pageSize); 
		List<Discuss> contentList = discussDao.getAllDiscuss(map);
		for(Discuss discuss : contentList){ 
			
			if(("hotel").equals(discuss.getOwnerType())){ 
				//酒店名字
				Hotel hotel =hotelRoomBiz.getHotelById(discuss.getOwner());
				if(hotel!=null){
					discuss.setOwnerName(hotel.getName());
				}
			}else if(("product").equals(discuss.getOwnerType())){
				//产品
				Product product =productBiz.getProductById(discuss.getOwner());
				if(product!=null){
					discuss.setOwnerName(product.getName());
				}
				
			}else if(("att").equals(discuss.getOwnerType())){
				//景点
				Scenic scenic =scenicBiz.getScenicById(discuss.getOwner());
				if(scenic!=null){
					discuss.setOwnerName(scenic.getName());
				}
			}
			
		}
		Integer count = discussDao.getAllDiscussCount(map); 
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(contentList);
		pm.setPageSize(pageSize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pageSize - 1) / pageSize);
		return pm;
	}
	//根据id查询详情
	@Override
	public Discuss getDiscussById(String id)throws AppBusinessException {
		Discuss discuss = discussDao.getDiscussById(id);
		
		if(("hotel").equals(discuss.getOwnerType())){
			//酒店名字
			Hotel hotel =hotelRoomBiz.getHotelById(discuss.getOwner());
			if(hotel!=null){
				discuss.setOwnerName(hotel.getName());
			}
			
		}else if(("product").equals(discuss.getOwnerType())){
			//产品
			Product product =productBiz.getProductById(discuss.getOwner());
			if(product!=null){
				discuss.setOwnerName(product.getName());
			}
			
		}else if(("att").equals(discuss.getOwnerType())){
			//景点
			Scenic scenic =scenicBiz.getScenicById(discuss.getOwner());
			if(scenic!=null){
				discuss.setOwnerName(scenic.getName());
			}
		}
		
		return discuss;
	}
	//更新
	@Override
	public void updateDisById(String id,String isAudit)throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isAudit", isAudit);
		map.put("id", id);
		discussDao.updateDisById(map);
	}
	//点评审核通过+积分
	@Override
	public void pointByAduitDis(Integer point,String member)throws AppBusinessException {
		Point pointDis =new Point();
		pointDis.setId(idBiz.generateId("coolsk_point"));
		pointDis.setMember(member);
		pointDis.setPoint(point);
		pointDis.setRemark("点评奖励"+point+"积分");
		pointDao.insertPoint(pointDis);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", member);
		Member memberOwner=memberDao.getMemberByMap(map);
		memberOwner.setCountPoint(memberOwner.getCountPoint()+point);
		memberDao.update(memberOwner);
	}
	//增加点评
	@Override
	public void insterDis(Discuss discuss)throws AppBusinessException {
		discuss.setId(idBiz.generateId("coolsk_discuss"));
		discussDao.insert(discuss);
	}
	/**
	 * author BruceZhang
	 * @param request
	 * 取得点评首页的内容
	 * @throws AppBusinessException
	 */
	@Override
	public void getIndexReview(HttpServletRequest request)
			throws AppBusinessException {
		//热门导航 - 酒店
		List<Content> jiuDianDaoHang = contentBiz.getContentByChannel("3217bd46-5d8a-441e-8c6e-895a67ff050a", 9);
		request.setAttribute("jiuDianDaoHang", jiuDianDaoHang);
		//热门导航 - 景点
		List<Content> jingDianDaoHang = contentBiz.getContentByChannel("5b86616e-d3b6-417f-8c6d-275a25a3b7d9", 9);
		request.setAttribute("jingDianDaoHang", jingDianDaoHang);
		//爽客热评
		List<Content> rePing = contentBiz.getDiscussByChannel("72cbc657-246e-474d-ab74-00ae77301963", 1);
		request.setAttribute("rePing", rePing);
		//精华点评
		List<Content> jingHuaDianPing = contentBiz.getDiscussByChannel("3d42860a-e77b-4847-bece-4e63e4e25ab4", 6);
		request.setAttribute("jingHuaDianPing", jingHuaDianPing);
		//点评心得
		List<Content> dianPingXinDe = contentBiz.getDiscussByChannel("da71540c-dce8-484e-bf0d-3e484bd1e9d8", 4);
		request.setAttribute("dianPingXinDe", dianPingXinDe);
		//广告位
		List<Content> alimama = contentBiz.getContentByChannel("16963161-e691-44fc-85e7-06f7fd19a7ba", 1);
		request.setAttribute("alimama", alimama);
		//攻略心得
		List<Content> gongLvXinDe = contentBiz.getContentByChannel("db6fdda8-1fc0-44a1-b828-8527bf94739a", 13);
		request.setAttribute("gongLvXinDe", gongLvXinDe);
		//本周热评
		List<Content> benZhouRePing = contentBiz.getDiscussByChannel("0ff37829-b921-43b4-a0e9-d36fb7a783fe", 4);
		request.setAttribute("benZhouRePing", benZhouRePing);
		//精彩热评
		List<Content> jingCaiRePing = contentBiz.getDiscussByChannel("aa161b0d-db51-4b41-9d2c-fd6086bd3c42", 3);
		request.setAttribute("jingCaiRePing", jingCaiRePing);
		//热门推荐
		List<Content> reMenTuiJian = contentBiz.getContentByChannel("9c0b3dd4-3ef1-4e82-8347-a1fec8ec1b6c", 24);
		request.setAttribute("reMenTuiJian", reMenTuiJian);
		//热门满意度(行程-满意)
	//	List<Content> manYiXingCheng = contentBiz.getDiscussByChannel("1d57ab8f-79f8-4b1b-99b9-493a2cec0962", 7);
	//	request.setAttribute("manYiXingCheng", manYiXingCheng);
		//热门满意度(行程-热门)
	//	List<Content> reMenXingCheng = contentBiz.getDiscussByChannel("9de16653-be98-439b-beed-77f845089860", 7); 
	//	request.setAttribute("reMenXingCheng", reMenXingCheng);
		//热门满意度(酒店-满意)
		List<Content> jiuDianManYi = contentBiz.getDiscussByChannel("c9b03dfb-0f1a-4138-8bcf-79298ab69973", 7);
		request.setAttribute("jiuDianManYi", jiuDianManYi);
		//热门满意度(酒店-热门)
		List<Content> jiuDianReMen = contentBiz.getDiscussByChannel("646ffa9c-e1a9-478e-95e6-644f1dcc7e0c", 7); 
		request.setAttribute("jiuDianReMen", jiuDianReMen);
		//热门满意度(旅游线路-满意)
		List<Content> xianLuManYi = contentBiz.getDiscussByChannel("51d44120-8f8a-43c6-a52a-e2ce1e091393", 7);
		request.setAttribute("xianLuManYi", xianLuManYi);
		//热门满意度(旅游线路-热门)
		List<Content> xianLuReMen = contentBiz.getDiscussByChannel("f3263a4c-57a3-4abe-9d8c-d4e69ee33cb9", 7);
		request.setAttribute("xianLuReMen", xianLuReMen);
		
		//热门满意度(景点-满意)
		List<Content> jingdianManYi = contentBiz.getDiscussByChannel("9ec0ca20-f334-47fd-95fa-09254b3b0ffa", 3);
		request.setAttribute("jingdianManYi", jingdianManYi);
		//热门满意度(景点-热门)
		List<Content> jingdianReMen = contentBiz.getDiscussByChannel("c6b02ab5-bc81-492a-93df-f30377a02ae3", 3);
		request.setAttribute("jingdianReMen", jingdianReMen);
		
		//热门满意度(旅游商品-满意)
		List<Content> shangPinManYi = contentBiz.getDiscussByChannel("1f2a20b9-ad43-440c-8c75-7e29dabe1394", 3);
		request.setAttribute("shangPinManYi", shangPinManYi);
		//热门满意度(旅游商品-热门)
		List<Content> shangPinReMen = contentBiz.getDiscussByChannel("b07e26c6-1ad7-44d9-a8f0-8a5c6c1376f5", 3);
		request.setAttribute("shangPinReMen", shangPinReMen);
		//帮助
		List<Content> help = contentBiz.getContentByChannel("dc0dfdb2-6656-4e47-ac95-0dda1f259497", 1);
		request.setAttribute("help", help);
		//底部
		List<Content> footer = contentBiz.getContentByChannel("eb2c95e7-a239-492b-93e1-8be0217cfe09", 1);
		request.setAttribute("footer", footer);
	}
	/**
	 * 查看底层页的点评 
	 * 
	 */
	@Override
	public  List<Discuss> getDiscussListByObj(Map<String, Object> map)throws AppBusinessException {
		return	discussDao.getDiscussListByObj(map);
	}
	/**
	 * 查看点评各分值的总条数
	 * 
	 */
	@Override
	public int getDiscussCountByObj(Map<String, Object> map)throws AppBusinessException {
		return	discussDao.getDiscussCountByObj(map);
	}
	
	
}
